function iFrameClickRefresh(obj, url) {
    var layer_iframe = layer.load();
    var oFrm = $('#iFrame-Content');
    oFrm.on('load',function () {
        layer.close(layer_iframe);
    });
    oFrm.attr("src", url);
    return false;
}

/**
 * 弹出iFrame
 * @param url
 * @param title
 */
function openLayerIFrame(url, title) {
    //iframe窗
    layer.open({
        type: 2,
        title: false,
        closeBtn: 0, //不显示关闭按钮
        shade: [0],
        area: ['340px', '215px'],
        offset: 'rb', //右下角弹出
        time: 1000, //2秒后自动关闭
        anim: 2,
        content: [url, 'no'], //iframe的url，no代表不显示滚动条
        end: function(){ //此处用于演示
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['80%', '90%'],
                content: url
            });
        }
    });
}

//dom加载完成后执行的js
$(function(){
    //  加载效果
    $(document).ajaxStart(function() { Pace.restart(); });

    /**
     * 编辑modal弹出框实现每次加载不同数据
     */
    $("#edit-modal").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });

    /**
     * 全选的实现
     */
    $(".check-all").click(function(){
        $(".ids").prop("checked", this.checked);
    });
    $(".ids").click(function(){
        var option = $(".ids");
        option.each(function(i){
            if(!this.checked){
                $(".check-all").prop("checked", false);
                return false;
            }else{
                $(".check-all").prop("checked", true);
            }
        });
    });

    /**
     * ajax-form
     * form class="ajax-form" method="post" action="xxx">
     */
    $(document).on('submit', 'form.ajax-form', function (e) {
        e.preventDefault();

        if ( $(this).hasClass('confirm') ) {
            var msg = $(this).attr('data-prompt-information');
            msg = msg != '' ? msg : '确认要执行该操作吗？';
            if(!confirm( msg )){
                return false;
            }
        }

        var form = $(this);
        $('[type=submit]', form).addClass('disabled');

        var loading = layer.load();

        $(".ajax-form .btn").addClass("disabled");

        var action = $(this).attr('action');
        var method = $(this).attr('method');

        if (!action) {
            return false;
        }
        if (!method) {
            method = 'get';
        }

        var formContent = $(this).serialize();
        obj = 'form.ajax-form';

        if (method == 'POST' || method == 'post') {
            $.post(action, formContent, function (result) {
                handleAjax(result, obj);
                $('[type=submit]', obj).removeClass('disabled');
                layer.close(loading);
            });
        }else if( method == 'GET' || method == 'get' ){
            $.get(action, formContent, function (result) {
                handleAjax(result, obj);
                $('[type=submit]', obj).removeClass('disabled');
                layer.close(loading);
            });
        }

        return false;
    });

    $('.ajax-get').click(function(){
        var target;
        var that = this;
        if ( $(this).hasClass('confirm') ) {
            var msg = $(this).attr('data-prompt-information');
            msg = msg != '' ? msg : '确认要执行该操作吗？';
            if(!confirm( msg )){
                return false;
            }
        }
        if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {
            $.get(target, {}, function (data) {
                console.log(data);
                handleAjax(data);
            }, 'json');
        }
        return false;
    });

    $('.ajax-post').click(function(){
        var target,query,form;
        var target_form = $(this).attr('data-target-form');
        var that = this;
        var nead_confirm=false;
        if( ($(this).attr('type')=='submit') || (target = $(this).attr('href')) || (target = $(this).attr('data-url')) ){
            form = $('.'+target_form);

            var msg = $(this).attr('data-prompt-information');
            msg = msg != '' ? msg : '确认要执行该操作吗？';

            if ($(this).attr('hide-data') === 'true'){//无数据时也可以使用的功能
                form = $('.hide-data');
                query = form.serialize();
            }else if (form.get(0)==undefined){
                return false;
            }else if ( form.get(0).nodeName=='FORM' ){
                if ( $(this).hasClass('confirm') ) {
                    if(!confirm(msg)){
                        return false;
                    }
                }
                if($(this).attr('url') !== undefined){
                    target = $(this).attr('url');
                }else{
                    target = form.get(0).action;
                }
                query = form.serialize();
            }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA') {
                form.each(function(k,v){
                    if(v.type=='checkbox' && v.checked==true){
                        nead_confirm = true;
                    }
                });
                if ( nead_confirm && $(this).hasClass('confirm') ) {

                    if(!confirm(msg)){
                        return false;
                    }
                }
                query = form.serialize();
            }else{
                if ( $(this).hasClass('confirm') ) {
                    if(!confirm(msg)){
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);

            $.post(target, query, function (result) {
                handleAjax(result, that);
                $(that).removeClass('disabled').prop('disabled',false);
            });
        }
        return false;
    });

    function handleAjax(result,obj)
    {
        layer.msg(result.msg);

        var interval = result.wait * 1000;
        if (result.code == 0) {
            if (obj) {
                // $(obj)[0].reset();
            }

            if (result.url == 'no-refresh') {
                return;
            }

            if (result.url == "refresh" || result.url == null || result.url == '') {
                setTimeout( function () {
                    location.href = result.url;
                    location.reload();
                }, interval);
            } else if (result.url) {
                setTimeout( function () {
                    location.href = result.url;
                }, interval);
            }

        } else {
            if( result.url != "refresh" && result.url != null && result.url != '' ){
                setTimeout( function () {
                    location.href = result.url;
                }, interval);
            }
        }
    }
});
